<?php
/**
 * Created by PhpStorm.
 * User: 87976
 * Date: 2018/3/1
 * Time: 14:14
 */

namespace App\Services\Permission;


use Spatie\Permission\Models\Permission;

/**
 * Class PermissionService
 * @package App\Services\Permission
 */
class PermissionService
{

    /**
     * @var Permission
     */
    protected $permission;


    /**
     * @var string
     */
    protected $guardName = 'admin';

    /**
     * PermissionService constructor.
     * @param Permission $permission
     */
    public function __construct(Permission $permission)
    {
        $this->permission = $permission;
    }

    /**
     * @author Jerry
     * @param string $guardName
     * @return $this
     */
    public function setGuardName(string $guardName)
    {
        $this->guardName = $guardName;

        return $this;
    }

    /**
     * @author Jerry
     * @return mixed
     */
    public function findPermissions()
    {
        return $this->permission->where('guard_name', $this->guardName)->get();
    }

    /**
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    public function findPermissionById(int $id)
    {
        return $this->permission->where('guard_name', $this->guardName)->where('id', $id)->first();
    }

    /**
     * @author Jerry
     * @param array $data
     * @return $this|\Illuminate\Database\Eloquent\Model
     */
    public function insertPermission(array $data)
    {
        return $this->permission->create($data);
    }

    /**
     * @author Jerry
     * @param array $data
     * @param int $id
     * @return bool
     */
    public function updatePermissionById(array $data, int $id)
    {
        return $this->permission->update($data, $id);
    }

    /**
     * @author Jerry
     * @param int $id
     * @return mixed
     */
    public function deletePermissionById(int $id)
    {
        return $this->permission->where('guard_name', $this->guardName)->where('id', $id)->delete();
    }


}